import {
  searchFoods,
  getAllCategories,
  getFoodsByCategory,
  addFood,
  deleteFood
} from '@/api/food'

export default {
  namespaced: true,

  state: {
    foods: [],
    categories: [],
    currentCategory: '',
    searchKeyword: ''
  },

  mutations: {
    SET_FOODS(state, foods) {
      state.foods = foods
    },
    SET_CATEGORIES(state, categories) {
      state.categories = categories
    },
    SET_CURRENT_CATEGORY(state, category) {
      state.currentCategory = category
    },
    SET_SEARCH_KEYWORD(state, keyword) {
      state.searchKeyword = keyword
    },
    REMOVE_FOOD(state, foodId) {
      state.foods = state.foods.filter(food => food.foodId !== foodId)
    }
  },

  actions: {
    // 搜索食物
    async searchFoods({ commit }, keyword) {
      try {
        const response = await searchFoods(keyword)
        if (response?.success) {
          commit('SET_FOODS', response.data || [])
          commit('SET_SEARCH_KEYWORD', keyword)
          commit('SET_CURRENT_CATEGORY', '')
        }
        return response
      } catch (error) {
        console.error('搜索食物失败:', error)
        throw error
      }
    },

    // 获取所有分类
    async fetchCategories({ commit }) {
      try {
        const response = await getAllCategories()
        if (response?.success) {
          commit('SET_CATEGORIES', response.data || [])
        }
        return response
      } catch (error) {
        console.error('获取分类失败:', error)
        throw error
      }
    },

    // 获取分类下的食物
    async getFoodsByCategory({ commit }, category) {
      try {
        const response = await getFoodsByCategory(category)
        if (response?.success) {
          commit('SET_FOODS', response.data || [])
          commit('SET_CURRENT_CATEGORY', category)
          commit('SET_SEARCH_KEYWORD', '')
        }
        return response
      } catch (error) {
        console.error('获取分类食物失败:', error)
        throw error
      }
    },

    // 添加食物
    async addFood({ dispatch }, food) {
      try {
        const response = await addFood(food)
        if (response?.success) {
          // 刷新当前列表
          if (this.state.food.currentCategory) {
            await dispatch('getFoodsByCategory', this.state.food.currentCategory)
          } else if (this.state.food.searchKeyword) {
            await dispatch('searchFoods', this.state.food.searchKeyword)
          }
        }
        return response
      } catch (error) {
        console.error('添加食物失败:', error)
        throw error
      }
    },

    // 删除食物
    async deleteFood({ commit }, foodId) {
      try {
        const response = await deleteFood(foodId)
        if (response?.success) {
          commit('REMOVE_FOOD', foodId)
        }
        return response
      } catch (error) {
        console.error('删除食物失败:', error)
        throw error
      }
    }
  },

  getters: {
    foods: state => state.foods,
    categories: state => state.categories,
    currentCategory: state => state.currentCategory,
    searchKeyword: state => state.searchKeyword
  }
} 